const {
  saveLinks,
  eraseLinkById,
  getLinks,
  createWork,
  eraseWork
} = require("../services/links.service.js");
const err = require("../constant/err.type.js");
class LinksController {
  async saveLink(ctx, next) {
    var res;
    try {
      const { ...links } = ctx.request.body;
      if(links.works){
        links.works = JSON.stringify(links.works)
      }
      if (!links.linkId) {
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "保存链路失败", data: "" }),
          ctx
        );
        return;
      }
      res = await saveLinks(links);
      if (!res) {
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "保存链路失败", data: "" }),
          ctx
        );
        return;
      } else {
        ctx.status = 200;
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: 0, msg: "ok", data: res }),
          ctx
        );
        return;
      }
    } catch (error) {
      console.log(error);
      ctx.app.emit(
        "error",
        err.setDefaultValue({
          code: -1,
          msg: "保存链路时出现错误，代码为" + error,
          data: "",
        }),
        ctx
      );
      return;
    }
  }
  async createLink(ctx, next) {
    var res;
    try {
      const { ...links } = ctx.request.body;
      res = await saveLinks(links);
      if (!res) {
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "创建链路失败", data: "" }),
          ctx
        );
        return;
      } else {
        ctx.status = 200;
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: 0, msg: "ok", data: res }),
          ctx
        );
        return;
      }
    } catch (error) {
      console.log(error);
      ctx.app.emit(
        "error",
        err.setDefaultValue({
          code: -1,
          msg: "保存链路时出现错误" + error,
          data: "",
        }),
        ctx
      );
      return;
    }
  }
  async eraseLink(ctx, next) {
    var res;
    var { linkId } = ctx.request.body;
    if (!linkId) {
      ctx.app.emit(
        "error",
        err.setDefaultValue({ code: -1, msg: "链路ID错误", data: "" }),
        ctx
      );
      return;
    }
    try {
      res = await eraseLinkById(linkId);
      if (!res) {
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "删除链路失败", data: "" }),
          ctx
        );
        return;
      } else {
        ctx.status = 200;
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: 0, msg: "ok", data: res }),
          ctx
        );
      }
    } catch (error) {
      console.log(error);
      ctx.app.emit(
        "error",
        err.setDefaultValue({
          code: -1,
          msg: "删除链路时出现错误" + error,
          data: "",
        }),
        ctx
      );
      return;
    }
  }
  async getLinks(ctx, next) {
    var { linkId, linkName,lineName,status,works,times,cancat } = ctx.request.body;
    var whereOpt = {};
    var res;
    try {
      linkId > 0 && Object.assign(whereOpt, { linkId });
      linkName && Object.assign(whereOpt, { linkName });
      lineName && Object.assign(whereOpt, { lineName });
      status && Object.assign(whereOpt, { status });
      works && Object.assign(whereOpt, { works });
      times && Object.assign(whereOpt, { times });
      cancat && Object.assign(whereOpt, { cancat });
      res = await getLinks(whereOpt);
      if (!res) {
        ctx.app.emit("error", err.companySearchError, ctx);
        return;
      }
      ctx.status = 200;
      ctx.app.emit(
        "error",
        err.setDefaultValue({ code: 0, msg: "ok", data: res }),
        ctx
      );
    } catch (error) {
      console.log(error);
      ctx.app.emit("error", err.companySearchError, ctx);
      return;
    }
  }
  async createWork(ctx,next){
    var {linkId,work,works} = ctx.request.body;
    var res;
    var obj
    try {
      linkId > 0 && Object.assign(obj, { linkId });
      work && Object.assign(obj, { work });
      works && Object.assign(obj, { works });
      res = await createWork(obj);
      if(!res){
        ctx.app.emit("error",err.setDefaultValue({code:-1,msg:"添加业务名称失败",data:""}),ctx);
        return;
      }
      ctx.status = 200;
      ctx.app.emit("error",err.setDefaultValue({code:0,msg:"添加业务名称成功",data:res}),ctx);
      return;
    } catch (error) {
      console.log(error);
      ctx.app.emit("error",err.setDefaultValue({code:-1,msg:"添加业务名称失败",data:""}),ctx);
        return;
    }
  }
  async eraseWork(ctx,next){
    var {linkId,work,works} = ctx.request.body;
    var res;
    var obj
    try {
      linkId > 0 && Object.assign(obj, { linkId });
      work && Object.assign(obj, { work });
      works && Object.assign(obj, { works });
      res = await createWork(obj);
      if(!res){
        ctx.app.emit("error",err.setDefaultValue({code:-1,msg:"删除业务名称失败",data:""}),ctx);
        return;
      }
      ctx.status = 200;
      ctx.app.emit("error",err.setDefaultValue({code:0,msg:"删除业务名称成功",data:res}),ctx);
      return;
    } catch (error) {
      console.log(error);
      ctx.app.emit("error",err.setDefaultValue({code:-1,msg:"删除业务名称失败",data:""}),ctx);
        return;
    }
  }
}

module.exports = new LinksController();
